package com.example.dbdoc.strategy;

import com.example.dbdoc.model.ColumnInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * MySQL数据库元数据查询策略
 * 
 * @author 系统生成
 * @version 1.0.0
 */
public class MySQLMetadataStrategy implements DatabaseMetadataStrategy {
    
    private static final Logger logger = LoggerFactory.getLogger(MySQLMetadataStrategy.class);
    
    @Override
    public String getTableComment(Connection connection, String tableName) {
        String tableComment = "";
        String sql = "SELECT TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";
        
        try {
            String schema = connection.getCatalog();
            try (PreparedStatement stmt = connection.prepareStatement(sql)) {
                stmt.setString(1, schema);
                stmt.setString(2, tableName);
                
                try (ResultSet rs = stmt.executeQuery()) {
                    if (rs.next()) {
                        tableComment = rs.getString("TABLE_COMMENT");
                        if (tableComment == null) {
                            tableComment = "";
                        }
                    }
                }
            }
            
            logger.debug("表 {} 的注释: {}", tableName, tableComment);
            
        } catch (SQLException e) {
            logger.warn("获取表 {} 注释时发生错误: {}", tableName, e.getMessage());
        }
        
        return tableComment;
    }
    
    @Override
    public void enhanceColumnInfo(Connection connection, String tableName, Map<String, ColumnInfo> columnMap) {
        String sql = "SELECT COLUMN_NAME, COLUMN_COMMENT, ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION";
        
        try {
            String schema = connection.getCatalog();
            try (PreparedStatement stmt = connection.prepareStatement(sql)) {
                stmt.setString(1, schema);
                stmt.setString(2, tableName);
                
                Map<String, ColumnInfo> orderedColumnMap = new LinkedHashMap<>();
                
                try (ResultSet rs = stmt.executeQuery()) {
                    while (rs.next()) {
                        String columnName = rs.getString("COLUMN_NAME");
                        String columnComment = rs.getString("COLUMN_COMMENT");
                        int ordinalPosition = rs.getInt("ORDINAL_POSITION");
                        
                        ColumnInfo column = columnMap.get(columnName);
                        if (column != null) {
                            column.setComment(columnComment);
                            orderedColumnMap.put(columnName, column);
                            logger.debug("字段排序: {} - 位置: {} - 注释: {}", columnName, ordinalPosition, columnComment);
                        }
                    }
                }
                
                columnMap.clear();
                columnMap.putAll(orderedColumnMap);
            }
            
        } catch (SQLException e) {
            logger.warn("获取表 {} 字段注释和顺序时发生错误: {}", tableName, e.getMessage());
        }
    }
}
